/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.ad.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.collect.ListUtils;
import com.jeesite.common.collect.MapUtils;
import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.shiro.realm.LoginInfo;
import com.jeesite.common.utils.excel.ExcelExport;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.ad.entity.AdAgent;
import com.jeesite.modules.ad.entity.AdVertiser;
import com.jeesite.modules.ad.service.AdAgentService;
import com.jeesite.modules.ad.service.AdVertiserService;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.utils.UserUtils;

/**
 * 广告主Controller
 * 
 * @author chenxm
 * @version 2019-04-16
 */
@Controller
@RequestMapping(value = "${adminPath}/ad/adVertiser")
public class AdVertiserController extends BaseController {

	@Autowired
	private AdVertiserService adVertiserService;

	@Autowired
	private AdAgentService adAgentService;

	@RequestMapping({ "checkAccount" })
	@ResponseBody
	public String checkAccount(String oldAccount, String account) {
		AdVertiser a;
		(a = new AdVertiser()).setAccount(account);
		if (account != null && account.equals(oldAccount)) {
			return "true";
		} else {
			return account != null && this.adVertiserService.getByAccount(a) == null ? "true" : "false";
		}
	}

	/**
	 * 获取数据
	 */
	@ModelAttribute
	public AdVertiser get(String id, boolean isNewRecord) {
		return adVertiserService.get(id, isNewRecord);
	}

	/**
	 * 查询列表
	 */
	@RequiresPermissions("ad:adVertiser:view")
	@RequestMapping(value = { "list", "" })
	public String list(AdVertiser adVertiser, Model model) {
		model.addAttribute("adVertiser", adVertiser);
		LoginInfo loginInfo = UserUtils.getLoginInfo();
		AdAgent curAgent = this.adAgentService.getByUserCode(loginInfo.getId());
		if (curAgent != null) {
			model.addAttribute("isAgent", 1);
		} else {
			model.addAttribute("isAgent", 0);
		}

		return "modules/ad/adVertiserList";
	}

	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("ad:adVertiser:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<AdVertiser> listData(AdVertiser adVertiser, HttpServletRequest request, HttpServletResponse response) {
		adVertiser.setPage(new Page<>(request, response));
		Page<AdVertiser> page = adVertiserService.findPage(adVertiser);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("ad:adVertiser:view")
	@RequestMapping(value = "form")
	public String form(AdVertiser adVertiser, Model model) {
		model.addAttribute("adVertiser", adVertiser);
		return "modules/ad/adVertiserForm";
	}
	
	/**
	 * 查看表单
	 */
	@RequiresPermissions("ad:adVertiser:view")
	@RequestMapping(value = "detail")
	public String detail(AdVertiser adVertiser, Model model) {
		model.addAttribute("adVertiser", adVertiser);
		return "modules/ad/adVertiserDetail";
	}

	/**
	 * 保存广告主
	 */
	@RequiresPermissions("ad:adVertiser:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated AdVertiser adVertiser) {
		adVertiserService.save(adVertiser);
		return renderResult(Global.TRUE, text("保存广告主成功！"));
	}

	/**
	 * 停用广告主
	 */
	@RequiresPermissions("ad:adVertiser:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(AdVertiser adVertiser) {
		adVertiser.setStatus(AdVertiser.STATUS_DISABLE);
		adVertiserService.updateStatus(adVertiser);
		return renderResult(Global.TRUE, text("停用广告主成功"));
	}

	/**
	 * 启用广告主
	 */
	@RequiresPermissions("ad:adVertiser:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(AdVertiser adVertiser) {
		adVertiser.setStatus(AdVertiser.STATUS_NORMAL);
		adVertiserService.updateStatus(adVertiser);
		return renderResult(Global.TRUE, text("启用广告主成功"));
	}

	/**
	 * 删除广告主
	 */
	@RequiresPermissions("ad:adVertiser:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(AdVertiser adVertiser) {
		adVertiserService.delete(adVertiser);
		return renderResult(Global.TRUE, text("删除广告主成功！"));
	}

	@RequiresPermissions("ad:adVertiser:edit")
	@RequestMapping(value = "treeData")
	@ResponseBody
	public List<Map<String, Object>> treeData() {
		List<Map<String, Object>> mapList = ListUtils.newArrayList();
		AdVertiser where = new AdVertiser();
		where.setStatus(AdVertiser.STATUS_NORMAL);
		Page page = new Page();
		page.setPageSize(-1);
		where.setPage(page);
		List<AdVertiser> list = this.adVertiserService.findPage(where).getList();
		for (AdVertiser agent : list) {
			Map<String, Object> map = MapUtils.newHashMap();
			map.put("id", agent.getId());
			map.put("pId", "-1");
			map.put("name", StringUtils.getTreeNodeName("2", agent.getEmail(), agent.getAccount()));
			mapList.add(map);
		}
		return mapList;
	}

	/**
	 * 导出广告主
	 */
	@RequiresPermissions("ad:adVertiser:edit")
	@RequestMapping(value = "exportData")
	public void exportData(AdVertiser adVertiser, Boolean isAll, String ctrlPermi, HttpServletResponse response) {
		// 是否查询子级
		adVertiser.getIndustryCode().setIsQueryChildren(true);
		User user = UserUtils.getUser();
		if (user.getMgrType().equals("0")) {
			// 不是管理员的情况,查询此登录用户对应的代理商的ID对应的广告主列表
			AdAgent adAgent = adAgentService.getCurAgent();
			if (adAgent != null) {
				adVertiser.setAgentId(adAgent.getId());
			}
		}
		List<AdVertiser> list = adVertiserService.findList(adVertiser);
		String fileName = "广告主信息" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
		try (ExcelExport ee = new ExcelExport("广告主信息数据", AdVertiser.class)) {
			ee.setDataList(list).write(response, fileName);
		}
	}
}